define(['jquery', 'bootstrap', 'backend', 'table', 'form','layui'], function ($, undefined, Backend, Table, Form, Layui) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'planrecords/index' + location.search,
                    add_url: 'planrecords/add',
                    edit_url: 'planrecords/edit',
                    del_url: 'planrecords/del',
                    multi_url: 'planrecords/multi',
                    import_url: 'planrecords/import',
                    table: 'planrecords',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'username', title: __('Username'), operate: 'LIKE'},
                        {field: 'datebetween', title: __('Datebetween'), operate: 'LIKE'},
                        {field: 'campaign', title: __('Campaign'), operate: 'LIKE'},
                        {field: 'reveal', title: __('Reveal')},
                        {field: 'click', title: __('Click')},
                        {field: 'consume', title: __('Consume'), operate:'BETWEEN'},
                        {field: 'clickaverage',title: '点击均价'},
                        {field: 'clickrate',title: '点击率'},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            table.on('load-success.bs.table', function (e, data) {
                $('.J_reveal').text(data.totalRow.reveal)
                $('.J_click').text(data.totalRow.click)
                $('.J_consume').text(data.totalRow.consume)
                $('.J_clickaverage').text(data.totalRow.clickaverage)
                $('.J_clickrate').text(data.totalRow.clickrate)
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            layui.use('laydate', function(){
                let laydate = layui.laydate;
                laydate.render({
                    elem: '#test-range',
                    range: ['#startDate','#endDate'],
                    done: function(value, date, endDate){
                        let dateArr = value.split(' - ');
                        $('#c-datebetween').val(dateArr.join('至'));
                    }
                })
            })
            Controller.api.bindevent();
        },
        edit: function () {
            layui.use('laydate', function(){
                let laydate = layui.laydate;
                laydate.render({
                    elem: '#test-range',
                    range: ['#startDate','#endDate'],
                    done: function(value, date, endDate){
                        let dateArr = value.split(' - ');
                        $('#c-datebetween').val(dateArr.join('至'));
                    }
                })
            })
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});